package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.AuthorMapper;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Title: TODO
 * @Description: TODO
 * @author: 丁丁
 * @date: 2021/3/12
 * @version:
 */
@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, ApAuthor> implements AuthorService {
    /**
     * 根据用户id查询作者信息
     *
     * @param id
     * @return
     */
    @Override
    public ApAuthor findByUserId(Integer id) {
        List<ApAuthor> list = this.list(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getId, id));
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    /**
     * 保存作者
     *
     * @param apAuthor
     * @return
     */
    @Override
    public ResponseResult insert(ApAuthor apAuthor) {
        this.save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据名称查询作者信息
     *
     * @param name
     * @return
     */
    @Override
    public ApAuthor selectAuthorByName(String name) {
        List<ApAuthor> list = list(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName, name));
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }
}
